package com.tanhua.manager.service;

import cn.hutool.core.date.DateTime;
import com.tanhua.domain.db.SoulOptions;
import com.tanhua.domain.db.SoulQuestion;
import com.tanhua.domain.db.SoulQuestionnaire;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.db.SoulApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SoulService {
    @DubboReference
    private SoulApi soulApi;

    /**
     * 查询所有试卷
     * @return
     */
    public ResponseEntity queryQuestionnaire() {
        List<SoulQuestionnaire> questionnaireList = soulApi.queryQuestionnaire();
        return ResponseEntity.ok(questionnaireList);
    }

    /**
     * 查询所有题目，分页
     * @return
     */
    public ResponseEntity queryQuestion(Integer page, Integer pagesize, String id) {
        PageResult pageResult = soulApi.queryQuestion(page, pagesize, id);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询所有选项，分页
     * @param page
     * @param pagesize
     * @param id
     * @return
     */
    public ResponseEntity queryOptions(Integer page, Integer pagesize, String id) {
        PageResult pageResult = soulApi.queryOptions(page, pagesize, id);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 保存题目
     *
     * @param questions
     * @return
     */
    public ResponseEntity saveQuestion(SoulQuestion questions) {
        //如果没有id，则说明是保存
        if (questions.getId() == null) {
            soulApi.saveQuestion(questions);
        }else { //如果有id，说明是修改
            soulApi.updateQuestion(questions);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 保存选项
     * @param
     * @return
     */
    public ResponseEntity saveOption(SoulOptions option) {
        if (option.getId() == null) {
            option.setCreated(new Date());
            option.setUpdated(new Date());
            soulApi.saveOption(option);
        } else {
            option.setUpdated(new Date());
            soulApi.updateOption(option);
        }

        return ResponseEntity.ok(null);
    }

    /**
     * 删除问题
     * @param id
     * @return
     */
    public ResponseEntity deleteQuestionById(Integer id) {
        //删除问题
        soulApi.deleteQuestionById(id);
        //删除问题对应的所有选项
        soulApi.deleteOptionsByQuestionId(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 删除单个选项
     * @param id
     * @return
     */
    public ResponseEntity deleteOptionById(Integer id) {
        //删除选项
        soulApi.deleteOptionById(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询单个问题
     * @param id
     * @return
     */
    public ResponseEntity queryQuestionById(Integer id) {
        SoulQuestion question = soulApi.queryQuestionById(id);
        return ResponseEntity.ok(question);
    }

    /**
     * 查询单个选项
     *
     * @param id
     * @return
     */
    public ResponseEntity queryOptionById(Integer id) {
        SoulOptions option = soulApi.queryOptionById(id);
        return ResponseEntity.ok(option);
    }
}
